﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NetFrameworkExtensions.BusinessLayer
{
  public static class CompareHelper
  {
    public static bool AreEqualsIdentified<T>(this T entity1, T entity2)
      where T : IIdentified
    {
      if (entity1 == null)
        return entity2 == null;
      if (entity2 == null)
        return false;

      if (!Object.Equals(entity1.Identifier, entity2.Identifier))
        return false;

      if (typeof(IConcurrent).IsAssignableFrom(typeof(T)))
        if (!((IConcurrent)entity1).Timestamp.AreEqualsArray(((IConcurrent)entity2).Timestamp))
          return false;

      return true;
    }

    public static bool AreEqualsIdentified<TIdentified, TIdentifier>(this TIdentified entity1, TIdentified entity2)
      where TIdentified : class, IIdentified<TIdentified, TIdentifier>
      where TIdentifier : IEquatable<TIdentifier>
    {
      if (entity1 == null)
        return entity2 == null;
      if (entity2 == null)
        return false;

      if (!entity1.Identifier.Equals(entity2.Identifier))
        return false;

      if (typeof(IConcurrent).IsAssignableFrom(typeof(TIdentified)))
        if (!((IConcurrent)entity1).Timestamp.AreEqualsArray(((IConcurrent)entity2).Timestamp))
          return false;

      return true;
    }
  }
}
